package com.newshub.monitor.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.newshub.common.Result;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import com.newshub.monitor.config.MonitorProperties;
import com.newshub.monitor.service.MonitorOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 监控概览控制器
 * 整合系统概览和性能监控功能
 */
@Slf4j
@RestController
@RequestMapping("/api/monitor/overview")
@CrossOrigin(origins = "*")
public class MonitorOverviewController {

    private final ObjectMapper objectMapper = new ObjectMapper();
    
    @Autowired
    private MonitorProperties monitorProperties;
    
    @Autowired
    private MonitorOverviewService monitorOverviewService;

    /**
     * 获取系统概览数据
     */
    @GetMapping("/system-overview")
    @BusinessLog(module = "监控概览", description = "获取系统概览数据", level = LogLevel.INFO)
    public Result<Map<String, Object>> getSystemOverview() {
        try {
            Map<String, Object> overview = monitorOverviewService.getSystemOverview();
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取系统概览失败", e);
            return Result.error("获取系统概览失败: " + e.getMessage());
        }
    }

    /**
     * 获取性能趋势数据
     */
    @GetMapping("/performance-trend")
    @BusinessLog(module = "监控概览", description = "获取性能趋势数据", level = LogLevel.INFO)
    public Result<Map<String, Object>> getPerformanceTrend(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "1h") String interval) {
        try {
            Map<String, Object> trend = monitorOverviewService.getPerformanceTrend(startTime, endTime, interval);
            return Result.success(trend);
        } catch (Exception e) {
            log.error("获取性能趋势失败", e);
            return Result.error("获取性能趋势失败: " + e.getMessage());
        }
    }

    /**
     * 获取方法性能排行
     */
    @GetMapping("/method-ranking")
    @BusinessLog(module = "监控概览", description = "获取方法性能排行", level = LogLevel.INFO)
    public Result<Map<String, Object>> getMethodRanking(
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            Map<String, Object> ranking = monitorOverviewService.getMethodRanking(limit, startTime, endTime);
            return Result.success(ranking);
        } catch (Exception e) {
            log.error("获取方法排行失败", e);
            return Result.error("获取方法排行失败: " + e.getMessage());
        }
    }

    /**
     * 获取系统状态
     */
    @GetMapping("/system-status")
    @BusinessLog(module = "监控概览", description = "获取系统状态", level = LogLevel.INFO)
    public Result<Map<String, Object>> getSystemStatus() {
        try {
            Map<String, Object> status = monitorOverviewService.getSystemStatus();
            return Result.success(status);
        } catch (Exception e) {
            log.error("获取系统状态失败", e);
            return Result.error("获取系统状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取实时监控数据
     */
    @GetMapping("/realtime-metrics")
    @BusinessLog(module = "监控概览", description = "获取实时监控数据", level = LogLevel.INFO)
    public Result<Map<String, Object>> getRealtimeMetrics() {
        try {
            Map<String, Object> metrics = monitorOverviewService.getRealtimeMetrics();
            return Result.success(metrics);
        } catch (Exception e) {
            log.error("获取实时监控数据失败", e);
            return Result.error("获取实时监控数据失败: " + e.getMessage());
        }
    }
}
